<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Model\Admin;
use Illuminate\Support\Facades\DB;
use Log;
use OSS\OssClient;
use OSS\Core\OssException;
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    #oss存储的地址
    public $point;
    public $bucket;
    /**
     * //创建阿里云对象
     * @return OssClient
     */
    public function oss(){
        $accessKeyId = "LTAIMMnKm7L3Xd3a";
        $accessKeySecret = "SapQnViPOb4DwzsJJ6YNLZHYFt4aV8";
        $endpoint = "http://oss-cn-beijing.aliyuncs.com";
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $this->point = "oss-cn-shanghai.aliyuncs.com";
        $this->bucket = "vipyipoint";
        return $ossClient;
    }
    /**
     * 公共数据列表函数
     * @param $table :表格名
     * @param $field :可搜索字段名
     * @param $field2 :不可搜索字段名
     * @param $button :根据表格内容获取相应的按钮
     * @param $info :从数据库获取的内容
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function dataList($table,$field,$field2,$button,$info)
    {
       return view('layout.data_list',['table'=>$table,'field'=>$field,'field2'=>$field2,'info'=>$info,'button'=>$button]);
    }

    /**
     * 获取状态按钮
     * @param $status :状态
     * @param $id     :内容id
     * @param $color  :按钮颜色
     * @param $word   :文字
     * @return string
     */
    public function getStatusBtn($status,$id,$color,$word)
    {
        $status == 0 ? $a=1 : $a=0;
        $res = '<a class="btn btn-'.$color.' btn-xs" href="javascript:setStatus('.$id.','.$a.')">'.$word.'<a>';
        return $res;
    }

    /**
     * @param $url      :按钮跳转地址
     * @param $id       :内容id
     * @param $color    :按钮颜色
     * @param $word     :文字
     * @return string
     */
    public function getBtn($url,$id,$color,$word)
    {
        $res = '<a href="'.$url.'/'.$id.'" class="btn btn-'.$color.' btn-xs" >'.$word.'</a>';
        return $res;
    }

    /**
     * @param $id   :内容id
     * @return string
     */
    public function getDeleteBtn($id)
    {
        $res = '<a class="btn btn-danger btn-xs" href="javascript:delProduct('.$id.')">删除<a>';
        return $res;
    }

    ###根据部门ID 获取下属所有部门
    public function getUserAllDep($dep_ids=[],&$department_arr=[]){
        $department_arr = array_merge($department_arr,$dep_ids);
        foreach ($dep_ids as $dep){
            $dep_arr = DB::table("department")->where("parentid",$dep)->pluck("id")->toArray();
            $this->getUserAllDep($dep_arr,$department_arr);
        }
        return array_flip(array_flip($department_arr));
    }

    ###根据DD_ID 获取用户下的所有部门和组
    public function getDeparUser($dd_id){
        ###特殊组
        $all_admin_arr = ['415863491662583833'];
        if(in_array($dd_id,$all_admin_arr)){
            //特殊权限组,查看所有
            $isLeader_dep = DB::table("department")->pluck("id","name")->toArray();
            $r_arr = [];
            foreach ($isLeader_dep as $key=>$dep){
                if($dep == 1){
                    continue;
                }
                $dd_ids = Admin::where("department","like","%".$dep."%")->pluck("dd_id")->toArray();
                $r_arr[$dep] = ['department'=>$key,"department_id"=>$dep,"dd_ids"=>$dd_ids,"is_leader"=>1];
            }
            return $r_arr;
            die();
        }
        $spacial = ['091654080232758206','485563258332'];//胡联英，杜晓旭
        if(in_array($dd_id,$spacial)){
            if($dd_id == $spacial["0"]){
                $res = DB::table("department")->whereIn("name",["西南一部","西南二部"])->pluck("id","name")->toArray();
                foreach($res as $key=>$val){
                    $dd_ids = Admin::where("department","[$val]")->pluck("dd_id")->toArray();
                    $r_arr[$val] = ['department'=>$key,"department_id"=>$val,"dd_ids"=>$dd_ids,"is_leader"=>1];
                }
                return $r_arr;
                die();
            }else{
                $res = DB::table("department")->whereIn("name",["华北二部","华北三部","华北四部","华北五部","华东事业群","中南事业群","东北事业群","西北事业群","UD项目","宗申项目"])->pluck("id","name")->toArray();
                foreach($res as $key=>$val){
                    $dd_ids = Admin::where("department","[$val]")->pluck("dd_id")->toArray();
                    $r_arr[$val] = ['department'=>$key,"department_id"=>$val,"dd_ids"=>$dd_ids,"is_leader"=>1];
                }
                return $r_arr;
                die();
            }
        }
        ###正常组
        $ad_dep = Admin::where("dd_id",$dd_id)->value("department");
        $ad_dep = json_decode($ad_dep,true);
        $r_arr = [];
        foreach ($ad_dep as $dep){
            $leader_info = DB::table("department")->where("id",$dep)->first();
            if(strpos($leader_info->managelist,$dd_id) !== false){
                $all_dep = $this->getUserAllDep($ad_dep);
                foreach($all_dep as $dep_t){
                    $dd_ids = Admin::where("department","like","%".$dep_t."%")->pluck("dd_id")->toArray();
                    $name = DB::table("department")->where("id",$dep_t)->value("name");
                    $r_arr[$dep_t] = ['department'=>$name,"department_id"=>$dep_t,"dd_ids"=>$dd_ids,"is_leader"=>1];
                }
            }else{
                $r_arr[$dep] = ['department'=>$leader_info->name,"department_id"=>$dep,"dd_ids"=>[$dd_id],"is_leader"=>0];
            }
        }
        return $r_arr;
    }

}
